import { Context, controller, inject, provide, post,del,put,plugin } from 'midway';
import { IUserService ,IInsertUser,IGetUser} from '../interface';
// import { get } from 'http';
@provide()
@controller('/')
export class UserController {

  @inject()
  ctx: Context;

  @plugin()
  jwt
  @inject('userService')
  service: IUserService;
  @inject()
  parsingToken
  /**
   * 注册用户
   */
  @post('/insertUser')
  async insertUser(): Promise<void> {
    const res= this.ctx.request.body;
    const user:IInsertUser={nickName:res.nickName,avatarUrl:res.avatarUrl,gender:res.gender,openid:(Number)((Math.random()*9+1)*100000)}
   await this.service.insertUser(user);
    this.ctx.body = {success: true, message: 'OK'};
  }
  @post('/getUser')
  // @get('/getUser/:id')
  async getUser():Promise<void> {
    //  console.log(this.ctx.header.authorization)
    const {params} = this.ctx.request.body
    console.log(params)
   const userList = await this.service.getUser(params)   
  //  const userList:any = await this.service.getUser()
  //  let list:any[] = []
  //  for (let item of userList){
  //    if(item.id!=id){
  //       list.push(item)
  //    }
  //  }
  const token:String = this.ctx.header.authorization
  const account = await this.parsingToken.parsing(token)
    console.log(account)
   this.ctx.body = {success: true, message: 'OK',data:userList};
  }
  @del('/delUser/:id')
  async delUser():Promise<void>{
    const id:number = this.ctx.params.id
    const blac = await this.service.delUser(id)
    if(blac==1&&blac!=undefined){
      this.ctx.body = {success: true, message: 'OK'};
    }else{
      this.ctx.body = {success: false, message: 'error'};
    }
  }
  @put('/putUser')
  async putUser(): Promise<void> {
    const res= this.ctx.request.body;
    const user:IGetUser={id:res.id,nickName:res.nickName,avatarUrl:res.avatarUrl,gender:res.gender,openid:res.openid}
   const statusCode= await this.service.putUser(user);
   if(statusCode==1&&statusCode!=undefined){
    this.ctx.body = {success: true, message: 'OK'};
  }else{
    this.ctx.body = {success: false, message: 'error'};
  }
  }
}
